/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-11-10
 *******************************************************************************/

package org.ayound.skin.editor.core.convert.style;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;

import org.apache.commons.io.FileUtils;
import org.ayound.skin.editor.core.convert.AbstractConverter;
import org.ayound.skin.editor.core.convert.ColorConverter;
import org.ayound.skin.editor.core.convert.helper.ColorHelper;
import org.ayound.skin.editor.core.convert.model.SkinModel;

public class StyleConverter extends AbstractConverter {

	public StyleConverter(SkinModel model) {
		super(model);
	}

	public void convert(String filePath,OutputStream output){

		Reader fileReader = null;
		BufferedReader bufferedreader = null;
		PrintWriter writer = null;
		try {
			fileReader = new FileReader(filePath);
			bufferedreader = new BufferedReader(fileReader);
			boolean isStart = false;
			boolean isEnd = true;
			String colorStr = "";
			StringBuffer strBuffer = new StringBuffer();
			ColorConverter converter = new ColorConverter(this.getModel());
			do {
				int charInt = fileReader.read();
				if (charInt == '\0' || charInt == -1) {
					break;
				}
				Character currChar = Character.toChars(charInt)[0];
				if (Character.isLetterOrDigit(currChar) || currChar == '#') {
					if (isStart && !isEnd) {
						colorStr += currChar;
					} else {
						// colorStr = "";
						strBuffer.append(currChar);
					}
				} else {
					if(currChar=='/'){
						strBuffer.append(colorStr);
					}else{
						if (ColorHelper.isColor(colorStr)) {
							int colorValue = converter.convert(colorStr);
							colorStr = ColorHelper.int2String(colorValue);
						}
						strBuffer.append(colorStr);
					}
					colorStr = "";
					if (currChar == ':') {
						isStart = true;
						isEnd = false;
						strBuffer.append(currChar);
					} else if (currChar == ';') {
						isEnd = true;
						isStart = false;
						strBuffer.append(currChar);
					} else if (currChar == '}') {
						isStart = false;
						isEnd = true;
						strBuffer.append(currChar);
					} else {
						strBuffer.append(currChar);
					}
				}
			} while (true);
			writer = new PrintWriter(output);
			writer.write(strBuffer.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bufferedreader != null) {
				try {
					bufferedreader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (writer != null) {
				writer.close();
			}

		}
	}

	public void convert(String filePath) {
		File fromFile = new File(filePath);
		File tempFile = new File(System.getProperty("java.io.tmpdir"),fromFile.getName());
		try {
			FileUtils.copyFile(fromFile, tempFile);
			convert(tempFile.getAbsolutePath(), new FileOutputStream(fromFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tempFile.delete();
	}

	public static void main(String[] args) {
		// StyleConverter scon = new StyleConverter(new SkinModel(30));
		// scon.convert("d:\\style-component.css");
	}
}
